package com.novellotus.iov.carvice.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.RxTaskListBean;
import com.novellotus.iov.carvice.interfaces.AdapterBottomInterface;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rich on 2017/7/10.
 */

public class TaskAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements AdapterBottomInterface {

    ItemClickInterface itemClickInterface;
    List<RxTaskListBean.RecordsBean> records;
    final int BOTTOM = 100;
    private boolean showBottom = false;

    public TaskAdapter(ItemClickInterface itemClickInterface, List<RxTaskListBean.RecordsBean> records) {
        this.itemClickInterface = itemClickInterface;
        this.records = records;
        if (records == null) {
            this.records = new ArrayList<>();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == records.size()) {
            return BOTTOM;
        } else {
            return 1;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == BOTTOM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_bottom, parent, false);
            return new BottomHolder(view);
        } else {
            View view = View.inflate(parent.getContext(), R.layout.layout_task_item, null);
            return new TaskItemHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (position == records.size()) {
            initBottomHolder((BottomHolder) holder, position);
        } else {
            if (records.get(position) != null) {
                initTaskHolder((TaskItemHolder) holder, position);
            }
        }


    }

    private void initTaskHolder(TaskItemHolder holder, final int position) {
        holder.tv_location.setText(MyUtils.getRealValue(records.get(position).getAddress()));
        holder.tv_vin.setText(MyUtils.getRealValue(records.get(position).getVin()));
        holder.tv_plate.setText(MyUtils.getRealValue(records.get(position).getPn()));
        holder.tv_time.setText(TimeUtils.stampTotime(records.get(position).getReportTime() + ""));
        holder.tv_type.setText(MyUtils.getRealValue(records.get(position).getFaults()));
        //status:已取消={0} 未处理={1} 处理中={2,3,4,5} 已完成={6}
        switch (records.get(position).getStatus()) {
            case "0":
                holder.tv_plate.setAlpha(0.6f);
                holder.tv_vin.setAlpha(0.6f);
                holder.img_finish.setVisibility(View.GONE);
                holder.tv_statue.setText("已取消");
                break;
            case "1":
                holder.tv_plate.setAlpha(1f);
                holder.tv_vin.setAlpha(1f);
                holder.img_finish.setVisibility(View.GONE);
                holder.tv_statue.setText("未处理");
                break;
            case "2":
            case "3":
            case "4":
            case "5":
                holder.tv_plate.setAlpha(1f);
                holder.tv_vin.setAlpha(1f);
                holder.tv_statue.setText("处理中");
                holder.img_finish.setVisibility(View.GONE);
                break;
            case "6":
                holder.tv_plate.setAlpha(0.6f);
                holder.tv_vin.setAlpha(0.6f);
                holder.tv_statue.setText("已完成");
                holder.img_finish.setVisibility(View.VISIBLE);
                break;
        }

        holder.rlt_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickInterface.clickItem(records.get(position));
            }
        });
    }

    private void initBottomHolder(BottomHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        if (showBottom) {
            return records.size() + 1;
        } else {
            return records.size();
        }

    }

    /**
     * @param records 刷新数据
     */
    public void update(List<RxTaskListBean.RecordsBean> records) {
        this.records = records;
        notifyDataSetChanged();
    }

    /**
     * @param records 添加下拉时的
     */
    public void addData(List<RxTaskListBean.RecordsBean> records) {
        this.records.addAll(records);
        notifyDataSetChanged();
    }

    @Override
    public void showBottomLayout() {
        showBottom = records.size() >= 5;

        notifyDataSetChanged();
    }

    @Override
    public void hideBottomLayout() {
        showBottom = false;
        notifyDataSetChanged();
    }

    class BottomHolder extends RecyclerView.ViewHolder {

        BottomHolder(View itemView) {
            super(itemView);
        }
    }

    class TaskItemHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_statue)
        TextView tv_statue;
        @BindView(R.id.img_finish)
        ImageView img_finish;
        @BindView(R.id.rlt_item)
        RelativeLayout rlt_item;
        @BindView(R.id.tv_type)
        TextView tv_type;
        @BindView(R.id.tv_location)
        TextView tv_location;
        @BindView(R.id.tv_plate)
        TextView tv_plate;
        @BindView(R.id.tv_vin)
        TextView tv_vin;
        @BindView(R.id.tv_time)
        TextView tv_time;

        public TaskItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ItemClickInterface {
        void clickItem(RxTaskListBean.RecordsBean record);
    }

}
